#include "widget.h"
#include "ui_widget.h"
#include <QShortcut>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //创建两个快捷键+-来控制滑动条
    QShortcut* cut1=new QShortcut(this);
    QShortcut* cut2=new QShortcut(this);
    cut1->setKey(QKeySequence("-"));
    cut2->setKey(QKeySequence("="));

    //将快捷键和滑动条的滑动关联上
    connect(cut1,&QShortcut::activated,this,&Widget::subValue);
    connect(cut2,&QShortcut::activated,this,&Widget::addValue);

}
Widget::~Widget()
{
    delete ui;
}

void Widget::subValue()
{
    //对滑动条进行减
    int value=ui->horizontalSlider->value();
    if(value<=ui->horizontalSlider->minimum())
    {
     return;
    }
    ui->horizontalSlider->setValue(value-5);
}


void Widget::addValue()
{
    //对滑动条进行加
    int value=ui->horizontalSlider->value();
    if(value>=ui->horizontalSlider->maximum())
    {
        return;
    }
    ui->horizontalSlider->setValue(value+5);

}


void Widget::on_horizontalSlider_valueChanged(int value)
{
   QString str("当前的值为:");
   str+=QString::number(value);
   ui->label->setText(str);
}
